package com.example.abbitmqtest.mq;

import com.example.abbitmqtest.config.RabbitConfig;
import com.example.abbitmqtest.entity.Order;
import com.example.abbitmqtest.service.OrderService;
import org.springframework.amqp.rabbit.annotation.RabbitListener;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

@Component
public class OrderDelayHandler {

    @Autowired
    private OrderService orderService;

    @RabbitListener(queues = RabbitConfig.ORDER_DELAY_QUEUE)
    public void handleOrderDelay(String orderNo) {
        // 查询订单
        Order order = orderService.getOrderByOrderNo(orderNo);
        
        // 如果订单存在且状态为待支付，则取消订单
        if (order != null && order.getStatus() == 0) {
            orderService.cancelOrder(orderNo);
        }
    }
}